// 第一个参数需要规划线路的点，第二个参数线路规划的一些选项（如工具（car）、线路选择(最短)），第三个参数规划出的线路的样式
// points参数：[[lat1,lng1],[lat2,lng2]] 或者 [{lat: lat1,lng: lng2},{lat: lat2,lng: lng2}] (可以多个点)
// planOptions参数：
// {
//  vehicle: foot | car | bike , 默认bike
//  weighting: fastest | shortest, 默认fastest
// }
var RouteUtil = {
    plan: function (points, planOptions, lineOptions) {

        if (!points) {
            return;
        }

        for (var i = 0; i < points.length; i++) {
            var point = points[i];
            console.log(point)
            if (point.lat) {
                point.lon = point.lng;
                continue;
            } else {
                point.lat = point[0];
                point.lng = point[1];
            }
        }

        var  data = JSON.stringify({
            points: points,
            hopper: planOptions
        });

        // 线路规划服务的请求
        var routPalnUrl = localhostPath + "/routePlan/plan";
        // var routPalnUrl = "http://61.145.229.217:9091/routePlan/plan";
        // $.post(routPalnUrl,data,function (result) {
        //             var polyline = new L.polyline(result.gpx, lineOptions);
        //             return polyline;
        // })
        // 发起请求
        var polyline;
        $.ajax({
            url:routPalnUrl,
            contentType :"application/json",
            type:"POST",
            data:data,
            async: false,
            success:function (result) {
                polyline = L.polyline(result.gpx,lineOptions);
            },
            error:function (msg) {
                alert('请求失败，' + 'Error code: ' + msg);
            }
        });
        return polyline;
    },
    carPlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'car', weighting: 'fastest'}, lineOptions);
        console.log(polyline)
        return polyline;
    },
    bikePlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'bike', weighting: 'fastest'}, lineOptions);
        return polyline;
    },
    footPlan: function (points, lineOptions) {
        var polyline = RouteUtil.plan(points, {vehicle: 'foot', weighting: 'fastest'}, lineOptions);
        return polyline;
    }
}